package com.icode.dao.impl;

import com.icode.core.dto.order.PageDTO;
import com.icode.core.dto.user.LoginDTO;
import com.icode.core.model.user.*;
import com.icode.dao.UserDao;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: sunk
 * Date: 14-12-26
 * Time: 下午3:45
 * To change this template use File | Settings | File Templates.
 */
@Repository("userDao")
public class UserDaoImpl extends EntityDaoImpl implements UserDao {
    @Override
    @Transactional
    public Customer findUserForLogin(LoginDTO loginDTO) {
        String hql = "from " + Customer.class.getName() + " e where e.username=:username and e.password=:password";
        Query query = currentSession().createQuery(hql);
        query.setParameter("username", loginDTO.getUserName());
        query.setParameter("password", loginDTO.getPassword());
        List<Customer> customers = query.list();
        return customers.isEmpty() ? null : customers.get(0);
    }

    @Override
    @Transactional
    public void saveOrUpdateUsersInfo(UserInfo usersInfo) {
//        usersInfo.init(usersInfo.province(), usersInfo.city(), usersInfo.district(), usersInfo.user());
        currentSession().saveOrUpdate(usersInfo);
    }

    @Override
    @Transactional
    public UserInfo findUsersInfoByPhoneNum(String phoneNum) {
        User user = findCustomerByUsername(phoneNum);
        String hql = "from " + UserInfo.class.getName() + " a where a.user=:user";
        Query query = currentSession().createQuery(hql);
        query.setParameter("user", user);
        List<UserInfo> list = query.list();
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public List<UserInfo> findUsersInfoByUsername(String username) {
        String hql = "from " + UserInfo.class.getName() + " a where a.username=:username";
        Query query = currentSession().createQuery(hql);
        query.setParameter("username", username);
        List<UserInfo> usersInfos = query.list();
        return usersInfos;
    }

    @Override
    @Transactional
    public List<Province> findProvince() {
        String hql = "from " + Province.class.getName();
        Query query = currentSession().createQuery(hql);
        List<Province> provinces = query.list();
        return provinces;
    }

    @Override
    public Province findProvinceById(String provinceId) {
        String hql = "from " + Province.class.getName() + " a where a.id=:id";
        Query query = currentSession().createQuery(hql);
        query.setParameter("id", Integer.parseInt(provinceId));
        return (Province) query.uniqueResult();
    }

    @Override
    @Transactional
    public List<City> findCityById(String provinceId) {
        String hql = "from " + City.class.getName() + " a where a.province=:provinceId";
        Query query = currentSession().createQuery(hql);
        Province province = (Province) currentSession().get(Province.class, Integer.parseInt(provinceId));
        query.setParameter("provinceId", province);
        List<City> cities = query.list();
        return cities;
    }

    @Override
    public City findCityByCityId(String cityId) {
        String hql = "from " + City.class.getName() + " a where a.id=:id";
        Query query = currentSession().createQuery(hql);
        query.setParameter("id", Integer.parseInt(cityId));
        return (City) query.uniqueResult();
    }

    @Override
    @Transactional
    public List<District> findDistrictById(String cityId) {
        String hql = "from " + District.class.getName() + " a where a.city=:cityId";
        Query query = currentSession().createQuery(hql);
        City city = (City) currentSession().get(City.class, Integer.parseInt(cityId));
        query.setParameter("cityId", city);
        List<District> districts = query.list();
        return districts;
    }

    @Override
    public District findDistrictByDistrictID(String districtId) {
        String hql = "from " + District.class.getName() + " a where a.id=:id";
        Query query = currentSession().createQuery(hql);
        query.setParameter("id", Integer.parseInt(districtId));
        return (District) query.uniqueResult();
    }

    @Override
    public Customer findByPhoneNum(String username) {
        String hql = "from " + Customer.class.getName() + " e where e.username=:username ";
        Query query = currentSession().createQuery(hql);
        query.setParameter("username", username);
        List<Customer> list = query.list();
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public Customer findCustomerByUsername(String username) {
        String hql = "from " + Customer.class.getName() + " u where u.username = :username and u.isActivate='true'";
        Query query = currentSession().createQuery(hql);
        query.setParameter("username", username);
        List<Customer> list = query.list();
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public Administrator findAdministratorByUsername(String username) {
        String hql = "from " + Administrator.class.getName() + " u where u.username = :username";
        Query query = currentSession().createQuery(hql);
        query.setParameter("username", username);
        List<Administrator> list = query.list();
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public UserInfo findUserInfoByUser(User user) {
        String hql = "from " + UserInfo.class.getName() + " a where a.user=:user";
        Query query = currentSession().createQuery(hql);
        query.setParameter("user", user);
        List<UserInfo> list = query.list();
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public UserInfo findUserInfoByUsername(String username, String userInfoGuid) {
        String hql = "from " + UserInfo.class.getName() + " a where a.username=:username and a.guid !=:guid";
        Query query = currentSession().createQuery(hql);
        query.setParameter("username", username);
        query.setParameter("guid", userInfoGuid);
        List<UserInfo> list = query.list();
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public void deleteObject(Object object) {
        currentSession().delete(object);
    }

    @Override
    public User findByUserName(String userName) {
        String hql = "from " + User.class.getName() + " a where a.username=:userName";
        Query query = currentSession().createQuery(hql);
        query.setParameter("userName", userName);
        List<User> list = query.list();
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public boolean userNameIsExist(String userName) {
        boolean b = false;
        String hql = "from " + User.class.getName() + " a where a.username=:userName";
        Query query = currentSession().createQuery(hql);
        query.setParameter("userName", userName);
        List<User> list = query.list();
        if (!list.isEmpty()) {
            b = true;
        }
        return b;
    }

    @Override
    public List<UserInfo> findAllUserInfo(String currentPage) {
        String hql = "from " + UserInfo.class.getName();
        Query query = currentSession().createQuery(hql);
        query.setFirstResult((Integer.parseInt(currentPage) - 1) * PageDTO.pageOfData);
        query.setMaxResults(PageDTO.pageOfData);
        List<UserInfo> list = query.list();
        return list;
    }

    @Override
    public List<UserInfo> findUserInfoByPhoneNumAndRealName(String phoneNum, String realName) {
        if (!"".equals(phoneNum) && !"".equals(realName)) {
            String hql = "from " + UserInfo.class.getName() + " a where a.user.username=:phoneNum and a.realName=:realName";
            Query query = currentSession().createQuery(hql);
            query.setParameter("phoneNum", phoneNum);
            query.setParameter("realName", realName);
            return query.list();
        } else if ("".equals(phoneNum) && !"".equals(realName)) {
            String hql = "from " + UserInfo.class.getName() + " a where a.realName=:realName";
            Query query = currentSession().createQuery(hql);
            query.setParameter("realName", realName);
            return query.list();
        } else {
            String hql = "from " + UserInfo.class.getName() + " a where a.user.username=:phoneNum";
            Query query = currentSession().createQuery(hql);
            query.setParameter("phoneNum", phoneNum);
            return query.list();
        }
    }

    @Override
    public void changeUserStatus(String phoneNum, String activate) {
        String hql = "update " + User.class.getName() + " a set a.isActivate=:activate where a.username=:phoneNum";
        Query query = currentSession().createQuery(hql);
        query.setParameter("activate", activate);
        query.setParameter("phoneNum", phoneNum);
        query.executeUpdate();
    }
}
